package com.kgcx.coupon.service;

import com.kgcx.coupon.constant.CouponConstant;
import com.kgcx.coupon.domain.AppointMutex;
import com.kgcx.coupon.domain.CouponDetail;
import com.kgcx.coupon.domain.MarketingPlan;
import com.kgcx.coupon.mapper.MarketingPlanMapper;
import com.kgcx.coupon.query.MarketingPlanQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 何凯
 * @version 0.1
 * @description
 * @time 2017/12/12 14:52
 * @since 0.1
 */
@Service
public class MarketingPlanService {
    @Autowired
    private MarketingPlanMapper marketingPlanMapper;
    @Autowired
    private AppointMutexService appointMutexService;
    @Autowired
    private CouponDetailService couponDetailService;

    /**
     * 保存营销计划
     * @param plan
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveMarketingPlan(MarketingPlan plan){
        if(null!=plan){
            marketingPlanMapper.insert(plan);
            if (plan.getMutualType().equals(CouponConstant.MarkePlan.POINTMUTEX)){
                 //保存互斥方案
                if(null!=plan.getAppointMutexList()&&plan.getAppointMutexList().size()>0){
                    for (AppointMutex appointMutex:plan.getAppointMutexList()) {
                        appointMutex.setPlanId(plan.getKid());
                    }
                    appointMutexService.insertByBatch(plan.getAppointMutexList());
                }
            }
            //保存优惠券明细
            if(null!=plan.getCouponDetailList()&&plan.getCouponDetailList().size()>0){
                for (CouponDetail detail :plan.getCouponDetailList()){
                    detail.setType(1);
                    detail.setPlanId(plan.getKid());
                }
                couponDetailService.insertByBatch(plan.getCouponDetailList());
            }

        }
    }

    /**
     * 修改营销方案
     * @param plan
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateMarketingPlan(MarketingPlan plan){
        if(null!=plan){
            marketingPlanMapper.updateByPrimaryKeySelective(plan);
            if (plan.getMutualType().equals(CouponConstant.MarkePlan.POINTMUTEX)){
                if(null!=plan.getAppointMutexList()&&plan.getAppointMutexList().size()>0){
                   //删除互斥方案
                    appointMutexService.deleteByPlanId(plan.getKid());
                    //保存互斥方案
                    appointMutexService.insertByBatch(plan.getAppointMutexList());
                }
            }
            if(null!=plan.getCouponDetailList()&&plan.getCouponDetailList().size()>0){
                //删除优惠券明细
                couponDetailService.deleteByPlanId(plan.getKid());
                //保存优惠券明细
                couponDetailService.insertByBatch(plan.getCouponDetailList());
            }
        }
    }

    /**
     * 分页查询营销计划
     * @param plan
     * @return
     */
    public List<MarketingPlan> queryMarketingPlanList(MarketingPlanQuery plan){
      return  marketingPlanMapper.queryMarketingPlanList(plan);
    }
    /**
     * 分页查询所有营销计划
     * @param plan
     * @return
     */
    public List<MarketingPlan> getAllMarketingPlan(MarketingPlanQuery plan){
      return  marketingPlanMapper.getAllMarketingPlan(plan);
    }
    /**
     * 获取营销计划明细
     * @param kid
     * @return
     */
    public MarketingPlan selectByPrimaryKey(Integer kid){
        return  marketingPlanMapper.selectByPrimaryKey(kid);
    }

}
